package com.m3958.logcollector.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.m3958.logcollector.domain.Person;
import com.m3958.logcollector.domain.Role;
import com.m3958.logcollector.repository.PersonRepository;
import com.m3958.logcollector.repository.RoleRepository;
import com.m3958.logcollector.util.common.M3958SecurityUtil;
import com.m3958.logcollector.vo.PersonInUserState;
import com.m3958.logcollector.vo.RoleVo;
import com.m3958.logcollector.vo.UserState;

@Component
public class UserStateFactory {

    @Autowired
    private RoleRepository roleRepo;

    @Autowired
    private PersonRepository personRepo;
    
    public UserState getUserState(boolean logined) {
        Person person = null;
        UserState us = new UserState(logined);

        if (logined) {
            person = personRepo.findOne(M3958SecurityUtil.getLoginPersonId());
            us.setUser(new PersonInUserState(person));
            List<Role> allRoles = roleRepo.findAll();
            us.setAllRoles(RoleVo.convertFromRoles(allRoles));
        }
        return us;
    }
}
